/**
 * 
 */
package org.compiere.apps.search;

import java.awt.Frame;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.adempiere.plaf.AdempierePLAF;
import org.compiere.apps.ALayout;
import org.compiere.minigrid.IDColumn;
import org.compiere.swing.CCheckBox;
import org.compiere.swing.CLabel;
import org.compiere.swing.CTextField;
import org.compiere.util.Env;
import org.compiere.util.Msg;

/**
 * @author Aslanbek
 *
 */
public class InfoBPMFormLine extends Info {

	private static final long serialVersionUID = -1210430300916874216L;

	/**
	 * @param frame
	 * @param modal
	 * @param WindowNo
	 * @param tableName
	 * @param keyColumn
	 * @param multiSelection
	 * @param whereClause
	 */
	public InfoBPMFormLine(Frame frame, boolean modal, int WindowNo, String tableName, String keyColumn, boolean multiSelection, String whereClause) {
		super(frame, modal, WindowNo, "FormLine", "BPM_FormLine_ID", multiSelection,	whereClause);
		
		log.info("Info BPM_FormLine");
		setTitle(Msg.getMsg(Env.getCtx(), "Info Form Line BPM"));
		
		try
		{
			statInit();
			p_loadedOK = initInfo ();
		}
		catch (Exception e)
		{
			return;
		}
		
		int no = p_table.getRowCount();
		setStatusLine(Integer.toString(no) + " " + Msg.getMsg(Env.getCtx(), "SearchRows_EnterQuery"), false);
		setStatusDB(Integer.toString(no));
		
		if (keyColumn != null && keyColumn.length() > 0)
		{
			executeQuery();
		}
		//
		pack();
		//	Focus
		fName.requestFocus();
		
	}
	
	private CLabel lName = new CLabel("Строка формы");
	private CTextField fName =new CTextField(20);
	private CLabel lFormName = new CLabel("Форма");
	private CTextField fFormName = new CTextField(20);
	private CLabel lOnlyForm11_12 = new CLabel("Только Формы 11 и 12");
	private CCheckBox flOnlyForm11_12 = new CCheckBox();

	@Override
	protected String getSQLWhere() {
		StringBuffer sql = new StringBuffer();
		if (fName.getText().length() > 0)
			sql.append(" AND UPPER(Name) LIKE ?");
		if(fFormName.getText().length() > 0)
			sql.append(" AND UPPER(FormName) LIKE ?");
//		if(flOnlyForm11_12.isSelected())
//			sql.append(" AND BPM_Form_ID IN (1000020, 1000021)");
		if(flOnlyForm11_12.isSelected())
			sql.append(" AND BPM_FormCode_ID IN (1000019, 1000020) AND BPM_FormColumn_ID IN (1001714,1000222)");

		return sql.toString();
	}

	@Override
	protected void setParameters(PreparedStatement pstmt, boolean forCount)	throws SQLException {
		int index = 1;
		if (fName.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fName));
		if(fFormName.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fFormName));
	}
	
	/**
	 *  Get SQL WHERE parameter
	 *  @param f field
	 *  @return Upper case text with % at the end
	 */
	private String getSQLText (CTextField f)
	{
		String s = f.getText().toUpperCase();
		if (!s.endsWith("%"))
			s = "%"+s+"%";
		log.fine( "String=" + s);
		return s;
	}
	
	
	private void statInit() throws Exception
	{				
		lFormName.setLabelFor(fFormName);
		lFormName.setAlignmentX(LEFT_ALIGNMENT);
		fFormName.setBackground(AdempierePLAF.getInfoBackground());
		fFormName.addActionListener(this);
		
		lName.setLabelFor(fName);
		fName.setBackground(AdempierePLAF.getInfoBackground());
		fName.addActionListener(this);
		
		lOnlyForm11_12.setLabelFor(flOnlyForm11_12);
		lOnlyForm11_12.setAlignmentX(LEFT_ALIGNMENT);
		flOnlyForm11_12.setBackground(AdempierePLAF.getInfoBackground());
		flOnlyForm11_12.addActionListener(this);

		parameterPanel.setLayout(new ALayout(2,2,true));
		
		parameterPanel.add(lFormName, null);
		parameterPanel.add(fFormName, null);
		
		parameterPanel.add(lName, null);
		parameterPanel.add(fName, null);
		
		parameterPanel.add(lOnlyForm11_12, null);
		parameterPanel.add(flOnlyForm11_12, null);
	}
	
	private boolean initInfo () throws SQLException
	{
		//  prepare table
		StringBuffer where = new StringBuffer(" IsActive='Y'");
//		prepareTable (BPM_FomLineLayout, "(SELECT BPM_Form.Name AS FormName, BPM_FormLine.* " +
//											"FROM BPM_FormLine, BPM_Form " +
//											"WHERE BPM_FormLine.BPM_Form_ID = BPM_Form.BPM_Form_ID) AS FormLine"
//				, where.toString(), "BPM_Form_ID");
		prepareTable (BPM_FomLineLayout, "(SELECT BPM_Form.Name AS FormName,BPM_Form.BPM_FormCode_ID, " +
				"BPM_FormValues.CellValue, BPM_FormValues.BPM_FormColumn_ID, BPM_FormLine.* " +
				"FROM BPM_FormLine, BPM_Form, BPM_FormValues WHERE BPM_FormLine.BPM_Form_ID = BPM_Form.BPM_Form_ID " +
				"AND BPM_FormValues.BPM_Form_ID = BPM_FormLine.BPM_Form_ID " +
				"AND BPM_FormValues.BPM_FormLine_ID = BPM_FormLine.BPM_FormLine_ID) AS FormLine"
, where.toString(), "BPM_Form_ID");
		return true;
	}
	private static final Info_Column[] BPM_FomLineLayout = {
		
		   new Info_Column("BPM_FormLine_ID", "BPM_FormLine_ID", IDColumn.class)
		 , new Info_Column("Форма","FormName", String.class)
		 , new Info_Column("Строка формы", "Name", String.class)
		 , new Info_Column("Название", "CellValue", String.class)  
		   
	};

}
